<?php namespace Siyuanliu\Filmcms\FormWidgets;

use Backend\Classes\FormWidgetBase;
use Snoopy\Snoopy;
use QL\QueryList;

/**
 * gatherTitle Form Widget
 */
class GatherTitle extends FormWidgetBase
{
    /**
     * {@inheritDoc}
     */
    protected $defaultAlias = 'siyuanliu_filmcms_gather_title';

    /**
     * {@inheritDoc}
     */
    public function init()
    {
    }

    /**
     * {@inheritDoc}
     */
    public function render()
    {
        $this->prepareVars();
        return $this->makePartial('gathertitle');
    }

    /**
     * Prepares the form widget view data
     */
    public function prepareVars()
    {
        $this->vars['name'] = $this->formField->getName();
        $this->vars['value'] = $this->getLoadValue();
        $this->vars['model'] = $this->model;
        $this->vars['result'] = null;

    }

    /**
     * {@inheritDoc}
     */
    public function loadAssets()
    {
        $this->addCss('css/gathertitle.css', 'siyuanliu.filmcms');
        $this->addJs('js/gathertitle.js', 'siyuanliu.filmcms');
    }

    /**
     * {@inheritDoc}
     */
    public function getSaveValue($value)
    {
        return $value;
    }

    /**
     * gather film data
     */
    public function onGather(){
      $title = input('Film.title');
      if (!$title) {
        throw new \ValidationException(['tittle' => 'siyuanliu.filmcms::lang.message.null_validatio']);
      }
      $snoopy = new Snoopy();
      $formvars['wd']= '湄公河';
      $formvars['sumbit']='search';
      $baseUrl = 'http://c.filmyun.cn/';
      $action = "http://c.filmyun.cn/index.php?m=vod-search";//表单提交地址
      $query = $snoopy->submit($action,$formvars);//$formvars为提交的数组
      $rules = array(
          //采集id为one这个元素里面的纯文本内容
          'title' => array('.xing_vb4>a','text'),
          //采集class为two下面的超链接的链接
          'href' => array('.xing_vb4>a','href'),
          // 'link' => array('.two>a','href'),
          // //采集class为two下面的第二张图片的链接
          // 'img' => array('.two>img:eq(1)','src'),
          // //采集span标签中的HTML内容
          // 'other' => array('span','html')
      );
      $detailsRules = array(
        //标题
        'title' => array('.vodh>h2','text'),
        //头像图片
        'avatar' => array('.lazy','src'),
        // 导演
        'director'=> array('.vodinfobox>ul>li:eq(0)>span','text'),
        // 演员
        'actor'=> array('.vodinfobox>ul>li:eq(1)>span','text'),
        // 分类
        'category'=> array('.vodinfobox>ul>li:eq(2)>span','text'),
        // 语言
        'language'=> array('.vodinfobox>ul>li:eq(3)>span','text'),
        // 地区
        'location'=> array('.vodinfobox>ul>li:eq(4)>span','text'),
        // 发行时间
        'publishDate'=> array('.vodinfobox>ul>li:eq(5)>span','text'),
        // rename
        'rename'=> array('.vodinfobox>ul>li:eq(6)>span','text'),
        //更新时间
        'updateDate'=> array('.vodinfobox>ul>li:eq(7)>span','text'),
        //描述
        'description'=>array('.vodplayinfo:first','text'),
      );
      $resourceRules = array(
        //播放源类型
        'resourcesType'=>array('.vodplayinfo>div>h3','text'),
        //播放源值
        'resources'=>array('.vodplayinfo>div>ul>li>input','value'),
      );
      $data = QueryList::Query($query->results,$rules)->data;
      $result = Array();
      $i = 0;
      foreach ($data as $list) {
        $filmlist =  $snoopy->fetch($baseUrl . $list['href']);
        $result[$i] = QueryList::Query($query->results,$detailsRules)->data[0];
        $resources = QueryList::Query($query->results,$resourceRules)->data;
        $result[$i]['resources'] = $resources;
        $i++;
      }
      //  print_r($result);die;
       return $this->vars['result'] = $result;
    }
}
